package com.itheima.health.common;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class PoiUtil {
    /**
     * 读入excel文件，解析后返回
     *
     * @param file
     * @throws IOException return List<String[]> List<每行的数据>
     */

    public static List<String[]> readExcel(MultipartFile file) throws IOException {
        List<String[]> list = null;
        //检查文件
        boolean bb = checkFile(file);
        if (bb) {
            list = analysisfile(file);
            return list;
        }
        return list;
    }


    //检查文件是否是模板文件
    private static boolean checkFile(MultipartFile file) throws IOException {

        //检查文件是否是模板文件
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename.equals("ordersetting_template.xlsx")) {
                return true;
            }
        }
        return false;
    }

    //解析数据
    private static List<String[]> analysisfile(MultipartFile file) throws IOException {
        List<String[]> list = new ArrayList<String[]>();
        //获取流
        InputStream inputStream = file.getInputStream();
        //创建工作簿
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        // 获取工作表
        XSSFSheet sheet = workbook.getSheetAt(0);
        //遍历工作表获得行对象
        for (Row row : sheet) {
            String[] cells = new String[row.getPhysicalNumberOfCells()];  //初始化数组长度
            int i = 0;
            //遍历行对象获取单元格对象
            for (Cell cell : row) {
                //获得单元格中的值
                //   String value = cell.getStringCellValue();
                String value = getCellValue(cell);
                System.out.println(value);
                log.info("Excel文件信息,{}", value);
                cells[i] = value;
                i++;
            }
            list.add(cells);
        }
        return list;
    }


    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        }
        //如果当前单元格内容为日期类型，需要特殊处理
        String dataFormatString = cell.getCellStyle().getDataFormatString();   //获取格式字符串  “General”
        if (dataFormatString.equals("m/d/yy")) {   //"01-八月-2022"
            //"01-八月-2022"  转换指定格式yyyy/MM/dd
            cellValue = new SimpleDateFormat("yyyy/MM/dd").format(cell.getDateCellValue());
            return cellValue;
        }
        //把数字当成String来读，避免出现1读成1.0的情况
        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {     //如果单元格是数字格式 避免出现1读成1.0的情况
            cell.setCellType(Cell.CELL_TYPE_STRING);            //把格式设置string格式
        }

        //判断数据的类型
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC: //数字
                cellValue = String.valueOf(cell.getNumericCellValue());   //获取单元格数字内容作为字符串
                break;
            case Cell.CELL_TYPE_STRING: //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK: //空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }

        return cellValue;
    }
}
